package com.example.li.myapplication;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.li.myapplication.dagger.HelloDagger;

import java.util.HashMap;

import javax.inject.Inject;

import rx.Observable;
import rx.Observable.OnSubscribe;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

public class MainActivity extends AppCompatActivity {
    private DownloadUtils utils;
    @Inject
    HelloDagger helloDagger;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }



    private void ieije() {
        Observable.just(1, 2, 3).subscribe(new Action1<Integer>() {
            @Override
            public void call(Integer integer) {
                Log.e("**", integer.toString());
            }
        });
        Observable.just("1", "2", "5", "77", "3", "6", "9", "7")
                .subscribeOn(Schedulers.newThread())
                .map(Integer::parseInt)
                .filter(s -> s > 1)
                .distinct()
                .take(3).
                subscribe(System.out::print);
    }

    private void abc() {
        Observable<String> observable = Observable.create(new OnSubscribe<String>() {
            @Override
            public void call(final Subscriber<? super String> subscriber) {
                final String data = "data";
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Thread.currentThread().sleep(10000);
                            Log.e("----1", Thread.currentThread().getName().toString());
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                            Log.e("执行", e.toString());
                        }
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                subscriber.onNext(data);
                            }
                        });

                    }
                }).start();

            }
        }).observeOn(AndroidSchedulers.mainThread());

        //   observable.observeOn(AndroidSchedulers.mainThread());
        Subscriber<String> subscriber = new Subscriber() {
            @Override
            public void onCompleted() {
            }

            @Override
            public void onError(Throwable e) {
            }

            @Override
            public void onNext(Object o) {
                Log.e("###", Thread.currentThread().getName().toString());
                ((TextView) (findViewById(R.id.text))).setText((String) o);
            }
        };

        observable.subscribe(subscriber);
    }

    private void laji() {
        utils = new DownloadUtils();
        utils.downloadImg("url").subscribeOn(Schedulers.io()).  //表示用网络访问 使用http协议获取数据  subscribe 订阅
                observeOn(AndroidSchedulers.mainThread()). //主线程 observe 观察
                subscribe(new Subscriber<byte[]>() {  //Subscriber 订户 用户
            @Override
            public void onCompleted() {
                //任务结束  显示对话框操作
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(byte[] bytes) {
                Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
                new ImageView(getApplicationContext()).setImageBitmap(bitmap);

                //操作data
            }
        });


        utils.downloadImg1("url", new HashMap<String, String>()).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<String>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(String s) {
// TODO: 2016/9/29 操作数据s

                    }
                });
    }



}
